package com.ctsi.commons.util.id;

import java.util.UUID;

/**
 * id产生
 * 
 * @author zhangjw
 *
 */
public class UUIDGenerator {

	private static final String digits(final long val,final  int digits) {
		long hi = 1L << (digits <<2);
		return Numbers.toString(hi | (val & (hi - 1)), Numbers.MAX_RADIX).substring(1);
	}

	private static final String UUIDToString(final UUID uuid) {
		StringBuilder sb = new StringBuilder(19);
		sb.append(digits(uuid.getMostSignificantBits() >> 32, 8));
		sb.append(digits(uuid.getMostSignificantBits() >> 16, 4));
		sb.append(digits(uuid.getMostSignificantBits(), 4));
		sb.append(digits(uuid.getLeastSignificantBits() >> 48, 4));
		sb.append(digits(uuid.getLeastSignificantBits(), 12));
		return sb.toString();
	}

	private static final String UUIDToStr(final UUID uuid) {
		StringBuilder sb = new StringBuilder(32);
		sb.append(digitsl(uuid.getMostSignificantBits() >> 32, 8));
		sb.append(digitsl(uuid.getMostSignificantBits() >> 16, 4));
		sb.append(digitsl(uuid.getMostSignificantBits(), 4));
		sb.append(digitsl(uuid.getLeastSignificantBits() >> 48, 4));
		sb.append(digitsl(uuid.getLeastSignificantBits(), 12));

		return sb.toString();
	}

	private static final String digitsl(final long val,final  int digits) {
		long hi = 1L << (digits <<2);
		return Long.toHexString(hi | (val & (hi - 1))).substring(1);
	}

	public static final String getNextSeqId() {

		return UUIDToString(java.util.UUID.randomUUID());

	}

	public final static String getId() {
		return UUIDToString(java.util.UUID.randomUUID());
	}

	public final static String getId(String name) {
		return UUIDToString(java.util.UUID.randomUUID());
	}

	public static final String getFastId() {

		return UUIDToStr(java.util.UUID.randomUUID());

	}

	 
    /*
     * 获取数据类型id
     */
	public static long getNumId() {
		return IdGen.get().nextId();
	}
}
